const osubmit = document.getElementById("btn_submit");
const opassword = document.getElementById("password");
const otel = document.getElementById("tel");

osubmit.onclick = function(){
     ajax("http://localhost:3000/api", function(res){
          res = JSON.parse(res);
          if(res.code === 0){
               alert(res.title)
               otel.value = "";
               otel.focus();
          }else if(res.code === 1){
               if(confirm("注册成功，是否跳转到登陆页面")){
                    location.href = "../logon.html"
               }
          }
     },{
          type:"register",
          username:otel.value,
          password:opassword.value
     })
}





function ajax(url, callback, data){
     let str = "";
     for(let i in data){
          str += `${i}=${data[i]}&`
     }
     url += "?" + str;
     const xhr = new XMLHttpRequest();
     xhr.open("get", url)
     xhr.onload = function(){
          if(xhr.status === 200){
               callback(xhr.responseText)
          }
     }
     xhr.send();
}